package mysql.statement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

public class Login01 {
    public static void main(String[] args) throws  Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户：");
        String username = scanner.next();
        System.out.println("请输入密码：");
        String password = scanner.next();

        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://master:3306/shujia",
                "root", "123456");
        Statement statement = connection.createStatement();
//      1.通过username去mysql中查找有没有这一条记录(存在：输入密码，不存在：报错)
//      2.存在之后，用查到的密码匹配输入的密码
        String sql="select * from user where username='"+username+"'";
        ResultSet rs = statement.executeQuery(sql);
        if(!rs.next()){
            System.out.println("用户输出错误");
//            结束
        }
//        匹配密码
        String password1 = rs.getString("password");
        if(password==null || !password.equals(password1)){
            System.out.println("密码不匹配失败");
        }else {
            System.out.println("登陆成功");
        }
        rs.close();
        statement.close();
        connection.close();
    }
}
